package interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Myintercepotor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,Object handler)throws Exception{
        String loginser=(String)request.getSession().getAttribute("loginer");
        System.out.printf(loginser);
        //判断当用户未登录的情况
        if (loginser==null){
            System.out.printf("用户未登录，进行拦截，返回登录页面进行登录");
            response.sendRedirect(request.getContextPath()+"/home/login.do");
            return false;
        }
        //用户已经登录
        System.out.printf("用户已经登录放行控制器方法");
        System.out.printf("控制器方法执行之前执行");
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

        System.out.printf("控制器方法执行之后执行");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }




}
